<?php
require_once(SITE_ROOT . 'class/db/DB.class.php');
require_once(SITE_ROOT . 'class/bean/Team.class.php');
require_once(SITE_ROOT . 'class/bean/Alliance.class.php');

class DBAllianceMember {
	
	public function getAllianceMembershipByAlliance($alliance) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare("SELECT `alliance#`, `team#` FROM `talliancemember` WHERE `alliance#` = ? ORDER BY `team#`")) {
			$stmt->execute(array($alliance->getAllianceID()));
			
			$stmt->bindColumn(1, $allianceID);
			$stmt->bindColumn(2, $teamID);
			
			$teams = array();
			
			while ($stmt->fetch()) {
				$team = new Team($teamID);
				$teams[$teamID] = $team;
			}
			
			return $teams;
		}
	}
	
	public function getAllianceMembershipByTeam($team) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `alliance#`, `team#` FROM `talliancemember` WHERE `team#` = ? ORDER BY `alliance#`')) {
			$stmt->execute(array($team->getTeamID()));
			
			$stmt->bindColumn(1, $allianceID);
			$stmt->bindColumn(2, $teamID);
			
			$alliances = new ArrayObject();
			
			while ($stmt->fetch()) {
				$alliance = new Alliance($allianceID);
				$alliances->append($alliance);
			}
			
			return $alliances;
		}
	}
	
	public function saveAllianceMembership($team) {
		$db = new DB();
		$connection = $db->connect();
		
		$alliances = $team->getAlliances();
		
		if ($stmt = $connection->prepare('REPLACE INTO `talliancemember` (`alliance#`, `team#`) VALUES (?, ?)')) {
			foreach ($alliances as $alliance) {
				$allianceID = $alliance->getAllianceID();
				$teamID = $team->getTeamID();
			
				$stmt->execute(array($allianceID, $teamID));				
			}
		}
	}
	
	public function saveAllianceMemberships($teams) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('REPLACE INTO `talliancemember` (`alliance#`, `team#`) VALUES (?, ?)')) {
			foreach ($teams as $team) {
				$alliances = $team->getAlliances();
		
				foreach ($alliances as $alliance) {
					$allianceID = $alliance->getAllianceID();
					$teamID = $team->getTeamID();
				
					$stmt->execute(array($allianceID, $teamID));				
				}
			}
		}
	}
	
	public function deleteAllianceMembership($alliance) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('DELETE FROM `talliancemember` WHERE `alliance#` = ?')) {
			$allianceID = $alliance->getAllianceID();
			
			$stmt->execute(array($allianceID));
		}
	}
	
	public function deleteAllianceMembershipByTeams($teams) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('DELETE FROM `talliancemember` WHERE `team#` = ?')) {
			foreach ($teams as $team) {
				$stmt->execute(array($team->getTeamID()));
			}
		}
	}
	
	public function deleteAllianceMembershipByTeamID($teamID) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('DELETE FROM `talliancemember` WHERE `team#` = ?')) {
			$stmt->execute(array($teamID));
		}
	}
	
}

?>